package com.itcast.demoone.adapter


import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.itcast.demoone.R

class MyAdapter(private val items: List<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {

    /**
     * 创建新的ViewHolder实例，该方法在RecyclerView首次显示时调用
     * @param parent 父布局
     * @param viewType 布局类型
     * @return ViewHolder 实例
     */
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
       // View.inflate()
       val view = LayoutInflater.from(parent.context).inflate(R.layout.item_layout, parent, false)
        return ViewHolder(view)
    }

    /**
     * 绑定数据到viewHolder
     * @param holder viewHolder实例
     * @param position 当前位置
     */
    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {
       holder.textView.text = items[position]
    }

    override fun getItemCount(): Int {
        return items.size
    }

    /**
     * 创建ViewHolder类, 用于缓存视图项中的控件
     */
    class ViewHolder(view: View): RecyclerView.ViewHolder(view){
       val textView: TextView = view.findViewById<TextView>(R.id.textView)
    }

}